package com.animee.week03.slice;

import com.animee.week03.ResourceTable;
import com.animee.week03.bean.User;
import com.animee.week03.db.DBManager;
import com.animee.week03.db.UserListProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ListContainer;
import ohos.data.rdb.RdbStore;
import ohos.data.resultset.ResultSet;

import java.util.ArrayList;
import java.util.List;

public class UserListAbilitySlice extends AbilitySlice {

    ListContainer userLc;
    List<User>mDatas = new ArrayList<>();
    private UserListProvider provider;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_userlist);
        initView();

        loadDBData();
    }

    private void loadDBData() {
        RdbStore rdbStore = DBManager.getRdbStore(this);
        String sql = "select * from usertb";
        ResultSet rs = rdbStore.querySql(sql, null);

        while (rs.goToNextRow()) {
            int id = rs.getInt(0);
            String name = rs.getString(1);
            String gender = rs.getString(2);
            String phone = rs.getString(3);
            String email = rs.getString(4);
            String info = rs.getString(5);
            User user = new User(id + "", name, phone, email, gender, info);
            mDatas.add(user);
        }
        provider.notifyDataChanged();
    }

    private void initView() {
        userLc = findComponentById(ResourceTable.Id_user_lc);
        provider = new UserListProvider(this, mDatas);
        userLc.setItemProvider(provider);
    }


}
